package com.kcht.sylyg08.service.impl;

import com.kcht.adp.common.log.Logger;
import com.kcht.sylyg08.dao.ITaskDao;
import com.kcht.sylyg08.service.ITaskService;
import com.kcth.sylyg08.entity.Task;
import com.kcth.sylyg08.entity.TaskStatus;

import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
  *
  * @Package:        com.kcht.sylyg08.service.impl
  * @Description:     java类作用描述
  * @Author:         lenovo
  * @CreateDate:     2019/5/15 14:21
 */
@Transactional(rollbackOn=Exception.class)
@ApplicationScoped
public class TaskServiceImpl implements ITaskService {
    @Inject
    private Logger logger;

    @EJB
    private ITaskDao tDao;

//    @EJB
//    private IMaterialContainerBoundDao mcbDao;

    @Override
    public Task changeTaskStatus(String containerId) {
        Task task = tDao.getTaskByContainerId(containerId);
        if (Objects.nonNull(task)) {
            task.setStatus(TaskStatus.完成);
            task.setCompleteTime(new Date());
            tDao.merge(task);
        } /*else {
            MaterialContainerBound mcBound = mcbDao.getByContainerId(containerId);
            *//*if (task.getCount() - mcBound.getCount() > 0) {

            }*//*
        }*/
        return task;
    }

    @Override
    public List<Task> changeOutGoingTaskStatus(String material) {
        List<Task> tasks = tDao.selectListByJPQL("select a from Task a where a.status=0 and a.taskType=2 and a.material=?", material);
        Date date = new Date();
        for (Task task : tasks) {
            task.setStatus(TaskStatus.完成);
            task.setCompleteTime(date);
            tDao.merge(task);
        }
        return tasks;
    }

    @Override
    public List<Task> getTasksByMaterial(String material) {
        return tDao.selectListByJPQL("select a from Task a where a.status=0 and a.taskType=2 and a.material=?", material);
    }

    @Override
    public Task getFirstUndoneOutGoingTaskByMaterial(String material) {
        return tDao.selectOneByJPQL("select a from Task a where a.taskType = 2 and a.status = 0 and a.material = ?", material);
    }
}
